package com.bosicen.crm.dao.sysmanage;

import java.util.List;


import org.springframework.stereotype.Component;
import com.bosicen.crm.dao.HibernateDao;
import com.bosicen.crm.dao.entity.Function;
import com.bosicen.crm.dao.entity.UserFunction;
import com.bosicen.crm.tools.AppUtil;
/**
 * 功能权限
 * @author tierlei
 *
 */
@Component("functionRoleDao")
@SuppressWarnings("unchecked")
public class FunctionRoleDao extends HibernateDao<Function, Integer> {

	/****
	 * 查询列表
	 * @param id
	 * @return
	 */
	
	public List<Function> findFunctionList(int id){
		return this.getSession().createQuery(" from Function ").list();
	}
	
	/**
	 * 查询单个功能
	 * @param id
	 * @return
	 */
	public Function findFunctionById(int id){
		return (Function) this.getSession().get(Function.class, id);
	}
	
	
	
	/***
	 * 删除
	 * @param c
	 */
	public void delFunction(Function c){
		this.getSession().createQuery("delete Function where id=：id").setParameter(0, c.getId()).executeUpdate();
	}
	
	/**
	 * 保存用户功能
	 * @param functionIds
	 * @param userid
	 * @return
	 */
	public boolean saveUserFunction(String functionIds,int userid){
		UserFunction ufunction = null;
		boolean b = false;
		try{
			for(String id:functionIds.split(",")){
				ufunction  = new UserFunction();
				ufunction.setFid(AppUtil.ObjectToInt(id));
				ufunction.setUid(userid);
				this.getSession().save(ufunction);
			}
			b = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return b;
	}
	
	/**
	 * 查询用户功能
	 * @param id
	 * @return
	 */
	public List<UserFunction> findUserFunctionById(int userid){
		return this.getSession().createQuery(" from UserFunction where uid=:userid").setParameter(0,userid).list();
	}
	
}
